package com.xnyzc.lhy.mis.service.impl.punishment;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.mis.entity.driver.wallet.OaDWallet;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.mapper.driver.wallet.OaDWalletMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.service.punishment.IOaDUserPenaltyTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OaDUserPenaltyTaskServiceImpl implements IOaDUserPenaltyTaskService {
    @Autowired
    private OaDUserMapper oaDUserMapper;
    @Autowired
    private OaDWalletMapper oaDWalletMapper;
    @Override
    @Transactional
    public Rv unsealDriverPenaltyTask() {
        QueryWrapper<OaDUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaDUser.ACCOUNT_STATUS, 5);
        List<OaDUser> oaDUsers  = oaDUserMapper.selectList(queryWrapper);
        if(oaDUsers.size() <= 0 || oaDUsers == null){
            return Rv.error(EErrorCode.infoIsNull);
        }
        Date date = new Date();
        for (OaDUser oaDUser : oaDUsers) {
             //如果当前时间 大于冻结时间
            if(oaDUser.getThawingTime()!= null){
                if(date.compareTo(oaDUser.getThawingTime())>0 ){
                    oaDUser.setAccountStatus(0);
                    oaDUserMapper.updateById(oaDUser);
                }
            }
        }
        return Rv.wrap(EErrorCode.success);
    }

    @Override
    @Transactional
    public Rv unsealDriverThawTask() {
        QueryWrapper<OaDWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaDWallet.LOGON_STATUS,1);
        List<OaDWallet> oaDWallets = oaDWalletMapper.selectList(queryWrapper);
        Date date = new Date();
        for (OaDWallet oaDWallet : oaDWallets) {
            if(date.compareTo(oaDWallet.getThawingTime())>0){
                oaDWallet.setLogonStatus(0);
                oaDWalletMapper.updateById(oaDWallet);
            }
        }
        return Rv.wrap(EErrorCode.success);
    }
}
